
const config = require('../config');
const { mongoose } = require('../db');

const schema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        autopopulate: true,
    },
    url: {
        type: String,
        required: true,
        unique: true,
        set: function (value) {
            if (value.indexOf('http') !== 0) {
                return 'http://' + value;
            }
            return value;
        }
    },
    nickname: {
        type: String,
        required: true,
        default: '神秘人',
    },
    avatar: {
        type: String,
        default: config.defaultAvatar,
    },
    bio: {
        type: String,
        default: '',
    },
    deletedAt: { type: Date, default: null },
}, {
    timestamps: true,
});

schema.plugin(require('mongoose-autopopulate'));
schema.plugin(require('mongoose-unique-validator'));
module.exports = mongoose.model('Friend', schema, 'friend');